<template>
  <div class="long">
      <el-form :model="ruleForm" status-icon  ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="用户名" prop="username">
    <el-input type="text" v-model="ruleForm.username" autocomplete="off" @blur="subul(ruleForm.username)"></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="password">
    <el-input type="password" v-model="ruleForm.password" autocomplete="off" @blur="subul(ruleForm.password)"></el-input>
  </el-form-item>
  <el-form-item label="验证码" prop="authcode" class="authcode">
    <el-input class="inp" v-model.number="ruleForm.authcode" @blur="subul(ruleForm.authcode)"></el-input>
    <el-button type="button" :plain="true" @click="sub()">获取验证码</el-button>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm(ruleForm)">提交</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
  </div>
</template>

<script>
// import <组件名称> from '<组件路径>'
import {getdata,getauthcode} from '../../api'
import {userinfo} from '../../utils/index'
// import jwt from 'jsonwebtoken'
  export default {
    name: 'Long',
    //import引入的组件需要注入到对象中才能使用
    components: {},
    data() {
      return {
    ruleForm: {
          username: '',
          password: '',
          authcode: '',
        },

      }
    },
    //监听属性 类似于data概念
    computed: {},
    //监控data中的数据变化
    watch: {},
    //方法集合
    methods: {
      subul(username){
         if(username===''){
              return  this.$message.error('信息不能为空');
           }
      },
         submitForm(formName) {
            if(this.ruleForm ===''){
              return  this.$message({
              message: '警告不能为空',
              type: 'warning'
            });
           }
            getdata(formName).then(resp=>{
            const {code,mess,data}=resp.data
            
            if(code===200){
              // 登录成功把加密名字保存到本地存储
              localStorage.setItem('token',data)
             this.$message({
             message: mess,
             type: 'success'
           });
          //  获取用户信息
             userinfo()
             this.$router.push('/')
            }else{
             this.$message.error(mess);
            }
        })
    //   }
          
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
    //   获取验证码
    sub(){
       getauthcode().then(resp=>{
        //    console.log()
            this.$message({
          message: `验证码${resp.data.data}`,
          center: true
        });
       })
    }
    
    },
    //生命周期 - 创建完成（可以访问当前this实例）
    created() {
        // this.get()
    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {},
    beforeCreate() {}, //生命周期 - 创建之前
    beforeMount() {}, //生命周期 - 挂载之前
    beforeUpdate() {}, //生命周期 - 更新之前
    updated() {}, //生命周期 - 更新之后
    beforeDestroy() {}, //生命周期 - 销毁之前
    destroyed() {}, //生命周期 - 销毁完成
    activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
  }
</script>
<style  scoped>
  /* //@import url(); 引入公共css类
   */
   .long{
       width: 500px;
       height: 500px;
       position: fixed;
       top: 50%;
       left: 50%;
       transform: translate(-50%,-50%);
   }
   .inp{
       width: 70%;
       /* background: cornflowerblue; */
   }
   .authcode{
       width: 100%;
       /* background: chocolate; */
      
       justify-content: space-between;
   }
   .el-form-item__content{
 display: flex;
       align-items: center;
   }
</style>